package com.itheima.web;
/**
 * Create By Awen on 2021/8/25
 *
 * @author Awen
 */

import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.AppJwtUtil;
import com.itheima.utils.BeanFactory;
import com.itheima.utils.CommonsUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    private UserService userService = (UserService) BeanFactory.getBean("UserService");

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = null;
        try {
            user = CommonsUtils.beanPopulate(User.class, request);
            User u = userService.login(user);
            if (u == null) {
                Result result = new Result(false, "用户名或密码错误", null);
                BaseController.responseJson(response, result);
            } else {
                // 登录成功后,生成token并返回给浏览器
                // 将用户信息保存到redis中,redis的键为id的值
                String token = AppJwtUtil.getToken(u.getId());
                System.out.println("登录成功，生成的token值： " + token);
                BaseController.responseJson(response, new Result(true, "登录成功", token));
            }
        } catch (Exception e) {
            e.printStackTrace();
            Result result = new Result(false, "服务器忙...", null);
            BaseController.responseJson(response, result);
        }
    }

    public void selectUserByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int currentPage = 1;
        int pageSize = 3;
        try {
            currentPage = Integer.parseInt(request.getParameter("currentPage"));
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        //        UserService userService = new UserService();
        PageBean<User> pb = userService.queryUsersByPage(currentPage, pageSize);
        System.out.println(pb);

        Result result = new Result(true, "", pb);
        BaseController.responseJson(response, result);

    }

    public void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            User user = BaseController.jsonStrToJavaObject(request, User.class);
            //            UserService userService = new UserService();
            userService.addUser(user);
            Result result = new Result(true, "添加成功", null);
            BaseController.responseJson(response, result);
        } catch (Exception e) {
            e.printStackTrace();
            Result result = new Result(false, "添加失败", null);
            BaseController.responseJson(response, result);
        }
    }

    public void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            User user = BaseController.jsonStrToJavaObject(request, User.class);
            System.out.println("user = " + user);
            userService.updateUserById(user);
            Result result = new Result(true, "修改成功", null);
            BaseController.responseJson(response, result);
        } catch (Exception e) {
            e.printStackTrace();
            Result result = new Result(false, "服务器忙...", null);
            BaseController.responseJson(response, result);
        }
    }


    public void deleteUserById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String id = request.getParameter("id");
            //            UserService userService = new UserService();
            userService.deleteUserById(id);
            Result result = new Result(true, "删除成功", null);
            BaseController.responseJson(response, result);
        } catch (Exception e) {
            e.printStackTrace();
            Result result = new Result(true, "删除失败", null);
            BaseController.responseJson(response, result);
        }
    }
}
